package ru.technogeek.labs.dreamnote.data;

import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class ItemDbAdapter {
	
	class ItemDbHelper extends SQLiteOpenHelper {

		public ItemDbHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(CREATE_NOTES_TABLE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			
		}
	}
	
	public ItemDbAdapter(Context context){
		this.context = context;
	}
	
    ///
    /// Context
    private final Context context;
    private ItemDbHelper helper;
    private SQLiteDatabase database;
    
	public static final String KEY_ITEMID = "_id";
    
    ///
	/// Tags
	/// Name: tags
	///
	public static final String KEY_TAGS = "tags";
	List<String> tags;
	public List<String> getTags(){
		return tags;
	}
	public void addTag(String tag) {
		tags.add(tag);
	}
	public String getDisplayTags() {
		String res = "";
		for(int i=0; i<tags.size(); i++){
			res += tags.get(i);
		}
		return res;
	}
	
	public int compareTo(ItemDbAdapter item) {
		return 0;
	}
	
	///
	/// Attachments
	/// Name:: attach
	///
	public static final String KEY_ATTACH = "attach";
	List<Attachment> attachments;
	public List<Attachment> getAttachments(){
		return attachments;
	}
	public void addAttachment(String path, String comment){
		attachments.add(new Attachment(path, comment));
	}
	
	public boolean hasAttachment(){
		return attachments.isEmpty();
	}
	
	///
	/// Date of creation
	/// Name: created
	///
	public static final String KEY_CREATED = "created";
	Date creationDate;
	public Date getCreationDate(){
		return creationDate;
	}
	public void setCreationDate(Date date){
		this.creationDate = date;
	}
	public String getDisplayCreationDate(){
		return new String(creationDate.getDay() + "/" + creationDate.getMonth() + "/" + creationDate.getYear());
	}
	
	///
	/// Date when event will be raised
	/// Name: fire
	///
	public static final String KEY_DATE = "fire";
	Date fireDate;
	public Date getFireDate(){
		return fireDate;
	}
	public void setFileDate(Date date){
		this.fireDate = date;
	}
	public String getDisplayFireDate(){
		return new String(fireDate.getDay() + "/" + fireDate.getMonth() + "/" + fireDate.getYear());
	}
	
	///
	/// Event length
	/// Name: length
	///
	public static final String KEY_LENGTH = "length";
	Date eventLength;
	public Date getEventLength(){
		return eventLength;
	}
	public void setEventLength(Date date){
		this.eventLength = date;
	}
	public String getDislayEventLength(){
		return new String(eventLength.getDay() + "/" + eventLength.getMonth() + "/" + eventLength.getYear());
	}

	///
	/// Event urgency
	/// Name: urgency
	///
	public static final String KEY_URGENCY = "urgency";
	int urgency;
	public int getUrgency(){
		return urgency;
	}
	public void setUrgency(int newUrgency){
		if(newUrgency > 0 || newUrgency <5)
			urgency = newUrgency;
	}
	
	///
	/// Event priority
	/// Name: priority
	///
	public static final String KEY_PRIORITY = "priority";
	int priority;
	public int getPriority(){
		return priority;
	}
	public void setPriority(int newPriority){
		if(newPriority > 0 || newPriority <5)
			priority = newPriority;
	}
	
	// TODO: Now live main note text as a String but we need to think about it closely later
	// it is important to made available 
	/// Name: text
	///
	public static final String KEY_TEXT = "text";
	private String text;
	private String dispalyText;
	
	public void setText(String text){
		this.text = text;
		
		int newLine = text.indexOf("\n");
		if(newLine < 0){
			if(text.length() < 20){
				dispalyText = text;
				return;
			} else {
				newLine = 20;
			}
		}
		
		dispalyText = text.substring(0, newLine);
	}
	
	public String getText() {
		return text;
	}
	
	public String getDisplayText() {
		return dispalyText;
	}
	
	/// DB Variables
    private static final String DATABASE_NAME = "daremnote";
    private static final String TABLE_NAME = "notes";
    private static final int DATABASE_VERSION = 1;
    private static final String CREATE_NOTES_TABLE = "create table " 
    	+ TABLE_NAME  
    	+ " (_id integer primary key autoincrement, "
    	+ KEY_TEXT + "text not null, "
    	+ KEY_TAGS + "text, "
    	+ KEY_ATTACH + "text, "
    	+ KEY_CREATED + "date not null, "
    	+ KEY_DATE + "date not null, "
    	+ KEY_LENGTH + "date not null, "
    	+ KEY_PRIORITY + "decimal, "
    	+ KEY_URGENCY+ "decimal);";

	

	/// 
	/// DatabaseFunctions
	///
	public ItemDbAdapter open() throws SQLiteException{
		helper = new ItemDbHelper(context);
		database = helper.getWritableDatabase();
		return this;
	}
	
	public void close(){
		helper.close();
	}
	
   public long createItem(String text, int priority, int urgency, 
		   Date firedate, Date length, Date created, 
		   List<String> attachments, List<String> tags) {
	   
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_TEXT, text);
        initialValues.put(KEY_PRIORITY, priority);
        initialValues.put(KEY_URGENCY, urgency);
        
        // TODO: Resolve date put
        //initialValues.put(KEY_DATE, firedate);
        //initialValues.put(KEY_LENGTH, length);
        //initialValues.put(KEY_CREATED, created);
        
        String strAttach = "";
        for(String attachment : attachments){
        	strAttach += attachment + "; ";
        }
        initialValues.put(KEY_ATTACH, strAttach);
        
        String strTags = "";
        for(String tag : tags) {
        	strTags += tag + "; ";
        }
        initialValues.put(KEY_TAGS, strTags);
        
        return database.insert(TABLE_NAME, null, initialValues);
    }
   
   
   public int deleteItem(int itemID) {
	   return database.delete(TABLE_NAME, KEY_ITEMID + "=" + itemID, null);
   }
}